"""Host views.


    # member
    # l
    # description
    # manager
    # *cn
    # *ipHostNumber

"""

from django import forms
from django.conf import settings
from hcp.auth.decorators import permission_required
from hcp.forms.custom import MyDynamicField, MyGroupedForm
from hcp.forms.grouped import FormSection
from hcp.forms.util import form_from_model
from hcp.jinja import render_to_response
from hcp.ldap_models import Host, update_model_from_post
from hcp.tracking import HcpCallback
from hcp.util.json import JsonResponse

textarea = forms.widgets.Textarea(attrs={'cols':40, 'rows': 4})

class HostForm(MyGroupedForm):

    class Required(FormSection):

        label = 'Required Attributes'

        cn = MyDynamicField(forms.CharField(), label='Name')
        ipHostNumber = MyDynamicField(forms.CharField(), label='IP Address')

    class Misc(FormSection):

        label = 'Misc'

        # XXX: What is member for?
        member = MyDynamicField(forms.CharField(), label='Member', required=False)
        l = MyDynamicField(forms.CharField(), label='Locality', required=False)
        description = MyDynamicField(forms.CharField(widget=textarea),
                                     label='Description',
                                     required=False)
        # XXX: Use "manager"?

@permission_required('manage_hosts')
def index(request):
    return render_to_response('hosts/index.html', request=request)

@permission_required('manage_hosts')
def host_list(request):
    hosts = Host.objects.all()
    data = [ {'host': host.cn,
              'ipHostNumber': host.ipHostNumber,
              'dn':host.dn} for host in hosts ]

    return JsonResponse({
        'hosts': data,
    })

@permission_required('manage_hosts')
def new(request):
    if request.method == 'POST':
        form = HostForm(request.POST)

        if form.is_valid():
            host = Host()
            fields = form.fields.keys()
            update_model_from_post(host, form.cleaned_data, fields)
            host.save(callback=HcpCallback(request.user.uid[0]))
            # XXX: Handle errors.
            # XXX: Make this a dialog instead of a separate page.
            return render_to_response('hosts/host_created.html', {'host': host}, request=request)
    else:
        form = HostForm()

    return render_to_response('hosts/new.html',
        {'form': form,
        }, request=request)

@permission_required('manage_hosts')
def edit(request, dn):
    host = Host.objects.get_dn(dn)
    # XXX not found

    if request.method == 'POST':
        form = HostForm(request.POST)
        if form.is_valid():
            update_model_from_post(host, form.cleaned_data, form.fields.keys())
            host.save(callback=HcpCallback(request.user.uid[0]))
            # XXX: Handle errors.

            return render_to_response('hosts/host_edited.html', {'host': host}, request=request)
    else:
        form = form_from_model(HostForm, host)

    return render_to_response('hosts/edit.html',
        {'host': host,
         'form': form,
        },
        request=request)

@permission_required('manage_hosts')
def remove(request, dn):
    host = Host.objects.get_dn(dn)
    host.delete(callback=HcpCallback(request.user.uid[0]))
    return JsonResponse({
        'result': 'success',
    })
